package com.mm.service.impl;

import com.mm.bo.GoodsInsertBo;
import com.mm.bo.GoodsSearchBo;
import com.mm.mapper.GoodsInfoMapper;
import com.mm.mapper.LogsInfoMapper;
import com.mm.org.n3r.idworker.Sid;
import com.mm.pojo.AdministratorInfo;
import com.mm.pojo.GoodsInfo;
import com.mm.pojo.LogsInfo;
import com.mm.redis.RedisService;
import com.mm.rediskey.AdminKey;
import com.mm.service.GoodsInfoService;
import com.mm.util.PageUtil;
import com.mm.vo.GoodsInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


/**
 * @ClassName GoodsServiceImpl
 * @Description TODO
 * @Author 任昊
 * @Date 2020/12/7 12:31
 */
@Service
public class GoodsInfoServiceImpl implements GoodsInfoService {

    @Resource
    private GoodsInfoMapper goodsInfoMapper;

    @Resource
    private Sid sid;

    @Resource
    private RedisService redisService;

    @Resource
    private LogsInfoMapper logsInfoMapper;

    /**
     * 新增物资
     * @param goodsInsertBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insertGoods(GoodsInsertBo goodsInsertBo) {
        //获取当前登录的管理员信息
        AdministratorInfo administratorInfoRedis = redisService.get(AdminKey.admin, goodsInsertBo.getToken(), AdministratorInfo.class);

        GoodsInfo goodsInfo = new GoodsInfo();
        BeanUtils.copyProperties(goodsInsertBo,goodsInfo);
        long currentTimeMillis = System.currentTimeMillis();
        goodsInfo.setGoodId(sid.nextShort());
        goodsInfo.setGoodCreateTime(currentTimeMillis);
        goodsInfo.setGoodUpdateTime(currentTimeMillis);
        int row = goodsInfoMapper.insertSelective(goodsInfo);

        //获取日志详情
        String log_detail = "管理员：" + administratorInfoRedis.getAdminRealName() + "新增了物资：" + goodsInsertBo.getGoodName();
        LogsInfo logsInfo = new LogsInfo(sid.nextShort(), administratorInfoRedis.getAdminId(), log_detail, currentTimeMillis, currentTimeMillis);
        logsInfoMapper.insertSelective(logsInfo);
        return row;
    }

    /**
     * 查询物资列表
     * @param goodsSearchBo
     * @return
     */
    @Override
    public PageUtil<GoodsInfoVo> selectGoods(GoodsSearchBo goodsSearchBo) {
        Integer count = goodsInfoMapper.findGoodsListCount(goodsSearchBo);
        List<GoodsInfoVo> goodsInfoVoList = goodsInfoMapper.findGoodsList(goodsSearchBo);
        PageUtil<GoodsInfoVo> PageUtil = new PageUtil<>(count, count, goodsSearchBo.getPageIndex(), goodsSearchBo.getPageSize(), goodsInfoVoList);
        return PageUtil;
    }
}
